﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using EsuApiLib;
using EsuApiLib.Rest;
using System.IO;


namespace AtmosClientWinForms
{
    public partial class MainForm : Form
    {

        private EsuApi m_esu = null;
        private ObjectPath m_currAtmosDir = null;
        private Byte[] m_fileToUpload = null;

        public MainForm()
        {
            InitializeComponent();
        }

        private void m_setAtmosConfigMenuItem_Click(object sender, EventArgs e)
        {
            AtmosSettingsForm settingsForm = new AtmosSettingsForm(m_esu);
            settingsForm.FormClosing += new FormClosingEventHandler(settingsForm_FormClosing);
            settingsForm.ShowDialog();
        }

        void settingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            m_esu = (sender as AtmosSettingsForm).EsuObject;
        }

        private void MainLoad(Object sender, EventArgs args)
        {

        }

        private void m_showDirectoriesMenuItem_Click(object sender, EventArgs e)
        {
            ObjectPath rootPath = new ObjectPath("/");
            List<DirectoryEntry> dirEntries = null;

            try
            {
                dirEntries = m_esu.ListDirectory(rootPath);
                m_dirTree.Nodes.Clear();
            }
            catch (EsuException esuEx)
            {
                String err = esuEx.ToString();
            }
            catch (Exception ex)
            {
                String err = ex.ToString();
            }

            m_dirTree.PathSeparator = "/";

            foreach (DirectoryEntry dir in dirEntries)
            {
                TreeNode node = new TreeNode();
                node.Tag = dir;
                node.Text = dir.Path.ToString();
                m_dirTree.Nodes.Add(node);

            }

            m_dirTree.NodeMouseClick += new TreeNodeMouseClickEventHandler(m_dirTree_NodeMouseClick);
            m_dirTree.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(m_dirTree_NodeMouseDoubleClick);


        }

        void m_dirTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs args)
        {
            //throw new NotImplementedException();
            ObjectPath searchPath = args.Node.Tag as ObjectPath;

            List<DirectoryEntry> dirContents = null;

            if (null != m_dirEntryListBox.Items)
            {
                m_dirEntryListBox.Items.Clear();
            }

            try
            {
                dirContents = m_esu.ListDirectory(m_currAtmosDir);
            }
            catch (EsuException esuEx)
            {
                String err = esuEx.ToString();
            }
            catch (Exception ex)
            {
                String err = ex.ToString();
            }

            foreach (DirectoryEntry entry in dirContents)
            {
                m_dirEntryListBox.Items.Add(entry.Path.ToString());
            }


        }


        void m_dirTree_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs args)
        {
            m_currAtmosDir = (args.Node.Tag as DirectoryEntry).Path;

        }

        private void m_exitToolStripMenuItem_Click(object sender, EventArgs args)
        {
            Application.Exit();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs args)
        {
            m_openFileDialog.FileOk += new CancelEventHandler(m_openFileDialog_FileOk);
            m_openFileDialog.ShowDialog();
        }

        void m_openFileDialog_FileOk(object sender, CancelEventArgs args)
        {
            Stream objectStream = m_openFileDialog.OpenFile();
            m_fileToUpload = new Byte[objectStream.Length];
            objectStream.Read(m_fileToUpload, 0, (int)objectStream.Length);
        }

        private void m_uploadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == m_fileToUpload || m_fileToUpload.Count() == 0)
            {
                return;
            }

            ObjectPath objPath = new ObjectPath(m_currAtmosDir.ToString() + m_openFileDialog.FileName.Substring(m_openFileDialog.FileName.LastIndexOf('\\') + 1));


            try
            {
                m_esu.CreateObjectOnPath(objPath, null, null, m_fileToUpload, null);
            }
            catch (EsuException esuEx)
            {
                String err = esuEx.ToString();
            }
            catch (Exception ex)
            {
                String err = ex.ToString();
            }


        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (null == m_dirEntryListBox.SelectedItem)
            {
                return;
            }

            String atmosFileToDelete = m_dirEntryListBox.SelectedItem.ToString();

            if (String.IsNullOrEmpty(atmosFileToDelete))
            {
                return;
            }

            ObjectPath atmosDeletePath = new ObjectPath(atmosFileToDelete);

            try
            {
                m_esu.DeleteObject(atmosDeletePath);
            }
            catch (EsuException esuEx)
            {
                String err = esuEx.ToString();
            }
            catch (Exception ex)
            {
                String err = ex.ToString();
            }
            

        }

    }
}
